package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.TranshipInfra;
import com.foreverwin.jzymes.common.mapper.CarrierScanLogAdviceMapper;
import com.foreverwin.jzymes.common.mapper.TranshipInfraAdviceMapper;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.service.IResourceTypeAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;

@WebSocketHandler(channel = "Z_DB_FLOW_LINE")
public class FlowLineHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IResourceTypeAdviceService resourceTypeAdviceService;
    @Autowired
    CarrierScanLogAdviceMapper carrierScanLogAdviceMapper;
    @Autowired
    TranshipInfraAdviceMapper transhipInfraAdviceMapper;

    private Map<String,String> activityRuleMap;

    public FlowLineHandler(){
        events = new ArrayList();
        events.add( "RESOURCE_STATUS_CHANGE" );
    }

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public void onAppEvent(AppEvent appEvent) {
        String event = appEvent.getName();
        if( "RESOURCE_STATUS_CHANGE".equals( event ) ){
            JSONObject eventData = (JSONObject) appEvent.getData();
            String site = eventData.getString( "SITE" );
            String resrce = eventData.getString( "RESRCE" );
            String status = eventData.getString( "STATUS" );

            JSONObject response = new JSONObject();
            response.put( "id", resrce );
            response.put( "status", status );
            sendMessage( "WS_RESOURCE_STATUS", response );
        }
    }

    @Override
    public JSONObject doReceiveAndReply( String messageType, JSONObject requestParams ) {

        JSONObject responseBody = null;
        if( "WS_INIT_DATA".equals( messageType ) ){
            responseBody = findFlowInfo();
        }

        return responseBody;
    }

    public JSONObject findFlowInfo(){

        String site = activityRuleMap.get( "SITE" );
        String clampResourceType = activityRuleMap.get( "CLAMP_RESOURCE_TYPE" );
        String inspectBarResourceType = activityRuleMap.get( "INSPECT_BAR_RESOURCE_TYPE" );
        String removeBarResourceType = activityRuleMap.get( "REMOVE_BAR_RESOURCE_TYPE" );
        String removeResourceType = activityRuleMap.get( "REMOVE_RESOURCE_TYPE" );
        String pFlowType = activityRuleMap.get( "P_FLOW_TYPE" );
        String mFlowType = activityRuleMap.get( "M_FLOW_TYPE" );

        Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );

        List<Map<String,Object>> dataList = new ArrayList<>();

        /**熟料接驳站数据*/
        String mFlowTypeBo = BOHandleUtil.buildResourceTypeHandle( site, mFlowType );
        List<TranshipInfra>  mTranshipInfraList = transhipInfraAdviceMapper.selectTranshipInfrasByDateRange( site, mFlowTypeBo, dateRange[0], dateRange[1] );
        for( TranshipInfra transhipInfra : mTranshipInfraList ){
            Map<String,Object> transhipMap = new HashMap<>();
            transhipMap.put( "id", transhipInfra.getResrce() );
            transhipMap.put( "okQty", transhipInfra.getProductQty() );
            transhipMap.put( "status", resourceAdviceService.findResourceStatus( site, transhipInfra.getResrce() ) );
            dataList.add( transhipMap );
        }

        /**生料接驳站数据*/
        String pFlowTypeBo = BOHandleUtil.buildResourceTypeHandle( site, pFlowType );
        List<TranshipInfra>  pTranshipInfraList = transhipInfraAdviceMapper.selectTranshipInfrasByDateRange( site, pFlowTypeBo, dateRange[0], dateRange[1] );
        for( TranshipInfra transhipInfra : pTranshipInfraList ){
            Map<String,Object> transhipMap = new HashMap<>();
            transhipMap.put( "id", transhipInfra.getResrce() );
            transhipMap.put( "okQty", transhipInfra.getRawQty() );
            transhipMap.put( "status", resourceAdviceService.findResourceStatus( site, transhipInfra.getResrce() ) );
            dataList.add( transhipMap );
        }

        /**全检前扫码*/
        List<Map<String,Object>> resrceCarrierList = null;
        resrceCarrierList = carrierScanLogAdviceMapper.countCarrierScanLogByResourceType( site, inspectBarResourceType, dateRange[0], dateRange[1] );
        for( Map<String,Object> carrierMap : resrceCarrierList ){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put( "id", carrierMap.get( "RESRCE" ) );
            dataMap.put( "okQty", carrierMap.get( "QTY" ) );
            dataMap.put( "ncQty", BigDecimal.ZERO );
            dataMap.put( "status", resourceAdviceService.findResourceStatus( site, (String) carrierMap.get( "RESRCE" )) );
            dataList.add( dataMap );
        }

        /**装夹机*/
        Map<String,Object> clampingTotalMap = new HashMap<>();
        clampingTotalMap.put( "id", "clampingTotal" );
        clampingTotalMap.put( "okQty", 0 );
        clampingTotalMap.put( "ncQty", 0 );
        dataList.add( clampingTotalMap );
        resrceCarrierList = carrierScanLogAdviceMapper.countCarrierScanLogByResourceType( site, clampResourceType, dateRange[0], dateRange[1] );
        for( Map<String,Object> carrierMap : resrceCarrierList ){
            boolean exist  = false;
            for( Map<String,Object> dataMap : dataList ){
                if( dataMap.get( "id" ).equals( carrierMap.get("RESRCE") ) ){
                    if( "1".equals( carrierMap.get( "RESULT" ) ) ){
                        dataMap.put( "okQty", carrierMap.get( "QTY" ) );
                        clampingTotalMap.put( "okQty", (Integer)clampingTotalMap.get( "okQty" ) + ( (BigDecimal)carrierMap.get( "QTY" ) ).intValue() );
                    }else{
                        dataMap.put( "ncQty", ((BigDecimal)dataMap.get( "ncQty" )).add( (BigDecimal)carrierMap.get( "QTY" ) ) );
                        clampingTotalMap.put( "ncQty", (Integer)clampingTotalMap.get( "ncQty" ) + ( (BigDecimal)carrierMap.get( "QTY" ) ).intValue() );
                    }
                    exist = true;
                    break;
                }
            }

            if( !exist ){
                Map<String,Object> dataMap = new HashMap<>();
                dataMap.put( "id", carrierMap.get( "RESRCE" ) );
                dataMap.put( "okQty", BigDecimal.ZERO );
                dataMap.put( "ncQty", BigDecimal.ZERO );
                dataMap.put( "status", resourceAdviceService.findResourceStatus( site, (String) carrierMap.get( "RESRCE" )) );
                if( "1".equals( carrierMap.get( "RESULT" ) ) ){
                    dataMap.put( "okQty", carrierMap.get( "QTY" ) );
                    clampingTotalMap.put( "okQty", (Integer)clampingTotalMap.get( "okQty" ) + ( (BigDecimal)carrierMap.get( "QTY" ) ).intValue() );
                }else{
                    dataMap.put( "ncQty", carrierMap.get( "QTY" ) );
                    clampingTotalMap.put( "ncQty", (Integer)clampingTotalMap.get( "ncQty" ) + ( (BigDecimal)carrierMap.get( "QTY" ) ).intValue() );
                }
                dataList.add( dataMap );
            }
        }


        /**拆夹前扫码*/
        Map<String,Object> dismantlingTotalMap = new HashMap<>();
        dismantlingTotalMap.put( "id", "dismantlingTotal" );
        dismantlingTotalMap.put( "okQty", 0 );
        dismantlingTotalMap.put( "ncQty", 0 );
        dataList.add( dismantlingTotalMap );
        resrceCarrierList = carrierScanLogAdviceMapper.countCarrierScanLogByResourceType( site, removeBarResourceType, dateRange[0], dateRange[1] );
        for( Map<String,Object> carrierMap : resrceCarrierList ){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put( "id", carrierMap.get( "RESRCE" ) );
            dataMap.put( "okQty", carrierMap.get( "QTY" ) );
            dataMap.put( "ncQty", BigDecimal.ZERO );
            dataMap.put( "status", resourceAdviceService.findResourceStatus( site, (String) carrierMap.get( "RESRCE" )) );
            dataList.add( dataMap );

            dismantlingTotalMap.put( "okQty", (Integer)dismantlingTotalMap.get( "okQty" ) + ( (BigDecimal)carrierMap.get( "QTY" ) ).intValue() );
        }

        /**拆夹机状态*/
        List<Map<String,String>> removeResourceList = resourceTypeAdviceService.findResourceList( site, removeResourceType );
        for( Map<String,String> removeResource : removeResourceList ){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put( "id", removeResource.get( "RESRCE" ) );
            dataMap.put( "status", resourceAdviceService.findResourceStatus( site, removeResource.get( "RESRCE" ) ) );
            dataList.add( dataMap );
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put( "date", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
        responseBody.put( "time", DateUtil.format( new Date(), "HH:mm" ) );
        responseBody.put( "data", dataList );

        return responseBody;
    }
}
